package com.example.amoxicilin.null_test.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Question;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class FavSearchActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    private List<String> favlist;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            String response=(String) msg.obj;
            favlist = JsonTools.jsonSplit(response);
            recyclerView.setAdapter(new RecyclerView.Adapter()
            {
                class MyViewHolder extends RecyclerView.ViewHolder{

                    public final ImageView clas;
                    public final TextView title;
                    public final TextView info;
                    public final ImageView upvote;
                    public final TextView upvotenumber;
                    public MyViewHolder(View itemView) {
                        super(itemView);
                        clas=(ImageView)itemView.findViewById(R.id.question_label);
                        title=(TextView)itemView.findViewById(R.id.fav_title);
                        info=(TextView)itemView.findViewById(R.id.fav_info);
                        upvote=(ImageView) itemView.findViewById(R.id.fav_upvote);
                        upvotenumber=(TextView)itemView.findViewById(R.id.fav_upvote_number);
                    }
                }


                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_fav, parent, false);
                    return new MyViewHolder(view);
                }

                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                    final MyViewHolder myViewHolder=(MyViewHolder) holder;
                    final Question question = JsonTools.getQuestion("question", favlist.get(position));
                    myViewHolder.title.setText(question.getName());
                    myViewHolder.upvotenumber.setText(""+question.getUpvote());

                    String temptext=question.getContent();
                    Spanned sp = Html.fromHtml(temptext, new Html.ImageGetter() {
                        @Override
                        public Drawable getDrawable(String source) {
                            InputStream is = null;
                            try {
                                is = (InputStream) new URL(source).getContent();
                                Drawable d = Drawable.createFromStream(is, "src");
                                d.setBounds(0, 0, d.getIntrinsicWidth(),
                                        d.getIntrinsicHeight());
                                is.close();
                                return d;
                            } catch (Exception e) {
                                return null;
                            }
                        }
                    }, null);
                    myViewHolder.info.setText(sp);
                    myViewHolder.info.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                    switch (question.getLabel1()){
                        case 1:
                            myViewHolder.clas.setImageResource(R.drawable.yi);break;
                        case 2:
                            myViewHolder.clas.setImageResource(R.drawable.ying);break;
                        case 3:
                            myViewHolder.clas.setImageResource(R.drawable.ji);break;
                        case 4:
                            myViewHolder.clas.setImageResource(R.drawable.fa);break;
                        case 5:
                            myViewHolder.clas.setImageResource(R.drawable.jian);break;
                        case 6:
                            myViewHolder.clas.setImageResource(R.drawable.ti);break;
                        case 7:
                            myViewHolder.clas.setImageResource(R.drawable.jing);break;
                        case 8:
                            myViewHolder.clas.setImageResource(R.drawable.dian);break;
                        default:break;
                    }
                    myViewHolder.upvote.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            myViewHolder.upvotenumber.setText(""+ (question.getUpvote()+1));
                            String path="http://120.24.94.172/JsonTestServer/jsonTest?type1=modify&type2=questionupvote&number="+question.getNumber();
                            String JsonString=HttpUtils.getJsonContent(path);
                            Status status=JsonTools.getStatus("status",JsonString);
                            if(status.getStatusId()==1){
                                Toast.makeText(FavSearchActivity.this,"点赞+1！",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    holder.itemView.setOnClickListener(new View.OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            int position=holder.getLayoutPosition();
                            //Toast.makeText(FavSearchActivity.this, "click"+position, Toast.LENGTH_SHORT).show();
                            Intent intent =new Intent();
                            intent.setClass(FavSearchActivity.this,QuestionActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("Content",question.getContent());
                            bundle.putInt("Number",question.getNumber());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });

                }

                @Override
                public int getItemCount() {
                    return favlist.size();
                }
            });
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_search);
        Bundle bundle=getIntent().getExtras();
        String search=bundle.getString("Search");
        recyclerView=(RecyclerView)findViewById(R.id.fav_search_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initData(search);
    }


    private void initData(String key){
        String path = "http://120.24.94.172/JsonTestServer/jsonTest?type1=query&type2=question&key=content&content="+key;
        String jsonString = HttpUtils.getJsonContent(path);
        Message message= new Message();
        message.obj=jsonString;
        handler.sendMessage(message);
    }

}
